﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using perfume.Models.DB;
using perfume.Models.ViewModels;

namespace perfume.Controllers
{
    public class ShoppingCartController : Controller
    {
        //
        // GET: /ShoppingCart/
        perfume_dbEntities per_db = new perfume_dbEntities();
        //
        // GET: /ShoppingCart/

        public ActionResult Index()
        {
            var cart = ShoppingCart.GetCart(this.HttpContext);
            // Set up our ViewModel
            var viewModel = new ShoppingCartViewModel
            {

                CartItems = cart.GetCartItems(),
                CartTotal = cart.GetTotal()
            };

            // Return the view
            return View(viewModel);
        }

        //
        // GET: /Store/AddToCart/5
        [Authorize]
        public ActionResult AddToCart(int id)
        {


            //Lay San Pham theo ID
            var add_product = per_db.products.Single(p => p.product_id == id);

            // Lay gio hang hien tai
            var cart = ShoppingCart.GetCart(this.HttpContext);
            if (Session["username"] != null)
            {
                cart.AddToCart(add_product);
                cart.UpdateAccountIntoCart(Session["username"].ToString());
                Session["cart_count"] = Convert.ToInt32(Session["cart_count"]) + 1;
            }
            else
            {
                Session["cart_count"] = Convert.ToInt32(Session["cart_count"]) + 1;
                cart.AddToCart(add_product);
            };


            // Chay toi trang gio hang
            return RedirectToAction("Index");
        }

        //
        // AJAX: /ShoppingCart/RemoveFromCart/5

        [HttpPost]
        public ActionResult RemoveFromCart(int id)
        {
            Session["cart_count"] = Convert.ToInt32(Session["cart_count"]) - 1;
            // Xóa Sản phẩm khỏi giỏ hàng
            var cart = ShoppingCart.GetCart(this.HttpContext);

            // Lấy tên sản phẩm để xuất ra trong thông báo xóa
            string productId = per_db.carts.Single(p => p.record_id == id).product.product_id.ToString();

            // Remove from cart
            int itemCount = cart.RemoveFromCart(id);

            // Display the confirmation message
            var results = new ShoppingCartRemoveViewModel
            {
                Message =
                    " Sản phẩm vừa mới được xóa khỏi giỏ hàng.",
                CartTotal = cart.GetTotal(),
                CartCount = cart.GetCount(),
                ItemCount = itemCount,
                DeleteId = id
            };
            RedirectToAction("Index","ShoppingCart");

            return Json(results);
        }

        //Http Post
        //Tang so luong san pham 
        [HttpPost]
        public ActionResult UpdateQuantity(int Id = 1, int quantity = 5)
        {
            //Lay san pham
            var product_increase = per_db.products.Single(p => p.product_id == Id);
            // Lay Gio Hang
            var cart = ShoppingCart.GetCart(this.HttpContext);
            cart.UpdateQuantity(product_increase, quantity);
            return RedirectToAction("Index");
        }

        //
        // GET: /ShoppingCart/CartSummary

        [ChildActionOnly]
        public ActionResult CartSummary()
        {
            var cart = ShoppingCart.GetCart(this.HttpContext);

            ViewData["CartCount"] = cart.GetCount();
            ViewBag.CartSum = cart.GetTotal();

            return PartialView("CartSummary");
        }
    }
}
